#include "telnet_server.h"
#include <iostream>
#include <thread>
#include <string>

void test1(std::string param1)
{
    std::cout << "test1: param1:" << param1 << std::endl;
    telPrint("test1: param1: %s", param1.c_str());

    telPrint2(1, 2, 3, 4);
    telPrint2("a");
    telPrint2("a", "ab");
    telPrint2('c');
}

void test2(std::string param1, std::string param2)
{
    std::cout << "test2: " << "param1:" << param1 << ", param2:" << param2 << std::endl;
    telPrint("test2: param1: %s , param2:%s\n", param1.c_str(), param2.c_str());
}

class CTest
{
public:
    void test3()
    {
        std::cout << "test3" << std::endl;
        telPrint("test3");
    }

    void test4(std::string param1, std::string param2)
    {
        std::cout << "test4:" << "param1:" << param1 << ", param2:" << param2 << std::endl;
        telPrint("test4: param1: %s , param2:%s\n", param1.c_str(), param2.c_str());
    }
};

CTest otest;
void regFuncs()
{
    TelnetServer::regist->attach<std::string>("t1", test1);
    TelnetServer::regist->attach<std::string, std::string>("t2", test2);
    TelnetServer::regist->attach<>("t3", &CTest::test3, otest);
    TelnetServer::regist->attach<std::string, std::string>("t4", &CTest::test4, otest);
}

int main(int argc, char* argv[])
{
    regFuncs();
    try
    {
        TelnetServer ts;
        std::thread([&ts]() {
            ts.start();
        }).detach();
        char c(' ');
        while (c != 'q' && c != 'Q')
        {
            std::cout << "Press q then enter to quit: \n";
            std::cin >> c;
        }
        ts.stop();
    }
    catch (std::exception& e)
    {
        std::cerr << "Telnet server error: " << e.what() << std::endl;
    }

    return 0;
}
